package com.lemon.secondhandplatform.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Good;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyGoodsAdapter extends RecyclerView.Adapter<MyGoodsAdapter.MyGoodHolder> {

    private OnMyGoodsItemClickListener onMyGoodsItemClickListener;

    private Context context;
    private List<Good> data;

    public MyGoodsAdapter(Context context, List<Good> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public MyGoodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_my_good, parent, false);
        return new MyGoodHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyGoodHolder holder, int position) {
        Good good = data.get(position);
        holder.bind(good);
    }


    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 刷新数据
     *
     * @param data
     */
    public void refresh(List<Good> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 追加数据
     *
     * @param data
     */
    public void append(List<Good> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void setOnMyGoodsItemClickListener(OnMyGoodsItemClickListener onMyGoodsItemClickListener) {
        this.onMyGoodsItemClickListener = onMyGoodsItemClickListener;
    }

    class MyGoodHolder extends RecyclerView.ViewHolder {

        private Good good;

        //商品名称
        @BindView(R.id.tvGoodName)
        TextView tvGoodName;
        //喜欢
        @BindView(R.id.tvLikesCounter)
        TextView tvLikesCounter;
        //商品略缩图
        @BindView(R.id.imgGoodPhoto)
        ImageView imgGoodPhoto;
        //商品介绍
        @BindView(R.id.tvGoodIntroduction)
        TextView tvGoodIntroduction;
        //评论
        @BindView(R.id.imgBtnComments)
        ImageView imgBtnComments;
        //编辑
        @BindView(R.id.tvEdit)
        TextView tvEdit;
        //删除
        @BindView(R.id.tvRemove)
        TextView tvRemove;

        public MyGoodHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Good good) {
            this.good = good;
            tvGoodName.setText(good.getName());
            Glide.with(context)
                    .load(good.getThumbnailURL())
                    .into(imgGoodPhoto);
            tvGoodIntroduction.setText(good.getIntroduction());
            tvLikesCounter.setText(good.getLikeCounter().toString());
        }

        /**
         * 查看评论
         * @param v
         */
        @OnClick(R.id.imgBtnComments)
        public void comments(View v) {
            if (onMyGoodsItemClickListener != null) {
                onMyGoodsItemClickListener.onCommentsClick(v, good);
            }
        }

        /**
         * 编辑
         * @param v
         */
        @OnClick(R.id.tvEdit)
        public void edit(View v) {
            if (onMyGoodsItemClickListener != null) {
                onMyGoodsItemClickListener.onEditClick(v, good);
            }
        }

        /**
         * 删除
         * @param v
         */
        @OnClick(R.id.tvRemove)
        public void remove(View v) {
            if (onMyGoodsItemClickListener != null) {
                onMyGoodsItemClickListener.onRemoveClick(v, good);
            }
        }
    }

    public interface OnMyGoodsItemClickListener {

        /**
         * 查看评论
         * @param v
         * @param good
         */
        void onCommentsClick(View v, Good good);

        /**
         * 删除
         * @param v
         * @param good
         */
        void onRemoveClick(View v, Good good);

        /**
         * 编辑
         * @param v
         * @param good
         */
        void onEditClick(View v, Good good);
    }
}
